﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Ink;
using System.Windows.Controls.Primitives;
using System.Globalization;


namespace UCUTextRecognizer
{
    /// <summary>
    /// Interaction logic for HandWrite.xaml
    /// </summary>
    /// 

    
    public partial class HandWrite : UserControl
    {
        private InkAnalyzer analyzer;
        private AnalysisStatus status;
        private int languages; // 0 - none, 1 - PL, 2 - EN, 3 - EN/PL
        private int currentLanguage;



        const int PL = 0x0415;
        const int EN = 0x0809;

        public HandWrite()
        {
            InitializeComponent();
            Start();
            this.languages = this.detectLanguages();
            currentLanguage = PL;
        }

        public void bindWithObject(Diagram.Element element,OurCanvas canvas,UMLObj umlObj) 
        {

        }

        private void changeStatusBar(String text)
        {
            progres.Content = text;
        }

        private int detectLanguages()
            {
            /*InkAnalyzer language_detector = new InkAnalyzer();
            StylusPointCollection points = new StylusPointCollection();
            points.Add(new StylusPoint(5.0, 5.0));
            Stroke stroke = new Stroke(points);
            language_detector.AddStroke(stroke, PL);*/
            //this.changeStatusBar(CultureInfo.CurrentCulture.LCID.ToString());

            return 0;
            }

    public void Start()
        {
            analyzer = new InkAnalyzer();

            //analyze.Click += new System.Windows.RoutedEventHandler(analyze_Click);

            canvas.StylusOutOfRange += new System.Windows.Input.StylusEventHandler(B_recognize_Click);
            canvas.Strokes.StrokesChanged += new StrokeCollectionChangedEventHandler(Strokes_StrokesChanged);
        }


        void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
        {
            if (e.Added.Count > 0)
            {
                analyzer.AddStrokes(e.Added);
                analyzer.SetStrokesType(e.Added, StrokeType.Writing);
                analyzer.SetStrokesLanguageId(e.Added, currentLanguage);

            }

            if (e.Removed.Count > 0)
            {
                analyzer.RemoveStrokes(e.Removed);
            }
            
        }

        public void setCanvasText(String text)
        {
            FormattedText ftext = new FormattedText(text, 
                                    CultureInfo.GetCultureInfo("en-us"),
                                    FlowDirection.LeftToRight,
                                    new Typeface("Verdana"),
                                    12,
                                    Brushes.Black);

            //Geometry _textGeometry = ftext.BuildGeometry(new Point(0, 0));

            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            drawingContext.DrawText(ftext, new Point(50,50));
            
            // Create a rectangle and draw it in the DrawingContext.
            //Rect rect = new Rect(new Point(160, 100), new Size(320, 80));
            //drawingContext.DrawRectangle(Brushes.LightBlue, (Pen)null, rect);
            
            canvas.Strokes.Draw(drawingContext);
            //drawingContext.Close();
            
            canvas.UpdateLayout();
        }

        public String getResult()
        {
            status = analyzer.Analyze();

            //changeStatusBar(status.Successful.ToString());

            if (status != null && status.Successful == true)
                return analyzer.GetRecognizedString();
                  
            return null;
        }


        private void B_recognize_Click(object sender, RoutedEventArgs e)
            {
            String temp = this.getResult();
            if (temp != null)
                changeStatusBar(this.getResult());
            }

        private void B_erase_Click(object sender, RoutedEventArgs e)
            {
            canvas.Strokes.Clear();
            changeStatusBar("");
            }

        private void B_language_EN_Click(object sender, RoutedEventArgs e)
            {
            this.currentLanguage = PL;
            B_language_EN.Visibility = Visibility.Hidden;
            B_language_PL.Visibility = Visibility.Visible;
            this.setCanvasText("22222222222222222");
            }

        private void B_language_PL_Click(object sender, RoutedEventArgs e)
            {
            this.currentLanguage = EN;
            B_language_EN.Visibility = Visibility.Visible;
            B_language_PL.Visibility = Visibility.Hidden;

            this.setCanvasText("1111111111111111111111111111111111111");
            }



    



    }
}
